package com.bw.a2209clubshop.ui.mall.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData

import com.bawei.a2209_mianshi.ui.mall.model.AdInfoModel
import com.bw.a2209clubshop.base.BaseResponse
import com.bw.a2209clubshop.base.BaseViewModel
import com.bw.a2209clubshop.base.state.Default
import com.bw.a2209clubshop.base.state.Empty
import com.bw.a2209clubshop.base.state.Failed
import com.bw.a2209clubshop.base.state.Loading
import com.bw.a2209clubshop.base.state.RequestEvent
import com.bw.a2209clubshop.base.state.Success
import com.bw.a2209clubshop.ui.mall.model.MallBanner
import com.bw.a2209clubshop.ui.mall.model.MallCate
import com.bw.a2209clubshop.ui.mall.model.MallGoods
import com.bw.a2209clubshop.ui.mall.model.MallModel
import com.bw.a2209clubshop.ui.mall.model.MallRowTypes
import com.bw.a2209clubshop.ui.mall.reposiitory.MallRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

/**
 *author：yjk
 *description:
 *date：2025/5/16
*/

@HiltViewModel
class MallViewModel @Inject constructor(private val mallRepository: MallRepository): BaseViewModel(){
//    private val _mallAdInfoStateFlow  by lazy {
//        MutableStateFlow<RequestEvent>(Default)
//    }
//    val mallAdInfoStateFlow = _mallAdInfoStateFlow.asStateFlow()
//
//    private val _mallAdInfoLiveData by lazy{
//        MutableLiveData<AdInfoModel>()
//    }
//    val mallAdInfoLiveData: LiveData<AdInfoModel> = _mallAdInfoLiveData

    private val _getMallDataEvent = MutableSharedFlow<RequestEvent>()
    val getMallDataEvent = _getMallDataEvent.asSharedFlow()

    private val _getMallBannerEvent = MutableSharedFlow<RequestEvent>()
    val getMallBannerEvent = _getMallBannerEvent.asSharedFlow()

    private val _getMallCateEvent = MutableSharedFlow<RequestEvent>()
    val getMallCateEvent = _getMallCateEvent.asSharedFlow()
    /** 品牌列表 */
    val bannerList by lazy {
        MutableLiveData<List<MallBanner>>()
    }
    /** 分类 */
    val cateListLiveData by lazy {
        MutableLiveData<List<MallCate>>()
    }

    /** 分类商品 */
    val goodsResultLiveData by lazy {
        MutableLiveData<List<MallGoods>>()
    }



    fun getMallData(){
        execute(
            action = mallRepository.getMallData(),
            onStart = {
                //开始请求
                _getMallDataEvent.emit(Loading)
            },
            onError = {
                //请求失败
                _getMallDataEvent.emit(Failed(it))
            },
            onFinish = {
                //请求结束
                if (it.data == null){
                    _getMallDataEvent.emit(Empty)
                }else{
                    _getMallDataEvent.emit(Success)
                    goodsResultLiveData.postValue(it.data)
                }
            }
        )
    }
    fun getMallBanner(){

        execute(
            action = mallRepository.getMallBanner(),
            onStart = {
                //开始请求
                _getMallBannerEvent.emit(Loading)
            },
            onError = {
                //请求失败
                _getMallBannerEvent.emit(Failed(it))
            },
            onFinish = {
                //请求结束
                if (it.data == null){
                    _getMallBannerEvent.emit(Empty)
                }else{
                    _getMallBannerEvent.emit(Success)
                    bannerList.postValue(it.data)
                }
            }
        )
    }
    fun getMallCate(){
        execute(
            action = mallRepository.getMallCate(),
            onStart = {
                //开始请求
                _getMallCateEvent.emit(Loading)
            },
            onError = {
                //请求失败
                _getMallCateEvent.emit(Failed(it))
            },
            onFinish = {
                //请求结束
                if (it.data == null){
                    _getMallCateEvent.emit(Empty)
                }else{
                    _getMallCateEvent.emit(Success)
                    cateListLiveData.postValue(it.data)
                }
            }
        )
    }
}